<template>
  <div class="WorkingSchedule" v-loading="loading">
    <el-form ref="form" size="mini" :model="form" label-position="top" class="myform">
      <div>
        <el-form-item label="加工单号/采购单号/款号/SKU" class="myform-item">
          <el-input v-model="form.number" placeholder="请输入加工单号/采购单号/款号/SKU" clearable @keyup.enter.native="onSearch"></el-input>
        </el-form-item>
        <el-form-item label="工厂名" class="myform-item">
          <el-input v-model="form.factory_name" placeholder="请输入工厂名" clearable @keyup.enter.native="onSearch"></el-input>
        </el-form-item>
      </div>
      <div>
        <el-form-item style="margin-top: 50px" class="myform-item">
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
    <el-table size="mini" :data="tableData" style="width: 100%" border :summary-method="getSummaries"
      show-summary>
      <el-table-column label="成品加工编码" align="center" prop="processing_code"></el-table-column>
      <el-table-column label="采购单号" align="center" prop="po_id"></el-table-column>
      <el-table-column label="款号" align="center" prop="type_no"></el-table-column>
      <el-table-column label="SKU" align="center" prop="sku_code"></el-table-column>
      <el-table-column label="计划数" align="center" prop="num">
        <template slot-scope="{ row }">
          <div style="color:red">
            {{row.num}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="实际数" align="center" prop="sj_num"></el-table-column>
      <el-table-column label="类型" align="center" prop="groups">
        <template slot-scope="{ row }">
          {{row.groups==1?"返厂维修":"采购退货"}}
        </template>
      </el-table-column>
      <el-table-column label="供应商" align="center" prop="factory_name"></el-table-column>
      <el-table-column label="发起时间" align="center" prop="createtime"></el-table-column>
      <el-table-column label="操作人" align="center" prop="adduser"></el-table-column>
    </el-table>
    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="postAdminOmsGetPurchaseReturnList"
    />
  </div>
</template>

<script>
import {
  postAdminOmsGetPurchaseReturnList
} from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      loading: false,
      form: {
        number: "",
        factory_name:"",
        page: 1,
        size: 10,
      },
      tableData: [],
      total: 1,
    };
  },
  activated() {
    let form = this.form;
    this.postAdminOmsGetPurchaseReturnList(form);
  },
  methods: {
    // 合计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (
          !values.every((value) => isNaN(value)) &&
          (column.label == "计划数" || column.label == "实际数")
        ) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index];
        } else {
          sums[index] = "";
        }
      });

      return sums;
    },
    // 采购列表
    async postAdminOmsGetPurchaseReturnList(data) {
      this.loading = true;
      this.form = data;
      postAdminOmsGetPurchaseReturnList(data)
        .then((res) => {
          this.loading = false;
          console.log("采购列表", res);
          this.savetime = res.savetime;
          this.tableData = res.data;
          this.total = Number(res.total);
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 搜索
    onSearch() {
      this.form.page = 1;
      this.form.size = 10;
      this.postAdminOmsGetPurchaseReturnList(this.form);
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.WorkingSchedule {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
}
</style>
